package com.nbcio.modules.crm.customer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbcio.common.core.domain.R;
import com.nbcio.modules.crm.customer.domain.CrmCustomerLevel;
import com.nbcio.modules.crm.customer.service.ICrmCustomerLevelService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * CRM客户等级管理控制器
 */
@Tag(name = "CRM客户等级管理")
@RestController
@RequestMapping("/crm/customer/level")
@RequiredArgsConstructor
public class CrmCustomerLevelController {

    private final ICrmCustomerLevelService levelService;

    /**
     * 分页查询客户等级
     */
    @GetMapping("/page")
    public R<IPage<CrmCustomerLevel>> page(
            @RequestParam int pageNum,
            @RequestParam int pageSize,
            @RequestParam(required = false) String levelCode,
            @RequestParam(required = false) String levelName,
            @RequestParam(required = false) String status) {
        LambdaQueryWrapper<CrmCustomerLevel> qw = new LambdaQueryWrapper<>();
        if (levelCode != null && !levelCode.isEmpty()) {
            qw.like(CrmCustomerLevel::getLevelCode, levelCode);
        }
        if (levelName != null && !levelName.isEmpty()) {
            qw.like(CrmCustomerLevel::getLevelName, levelName);
        }
        if (status != null && !status.isEmpty()) {
            qw.eq(CrmCustomerLevel::getStatus, Integer.parseInt(status));
        }
        qw.orderByAsc(CrmCustomerLevel::getSortOrder);
        IPage<CrmCustomerLevel> page = levelService.page(Page.of(pageNum, pageSize), qw);
        return R.ok(page);
    }

    /**
     * 查询所有客户等级（列表）
     */
    @GetMapping("/list")
    public R<List<CrmCustomerLevel>> list(@RequestParam(required = false) String status) {
        LambdaQueryWrapper<CrmCustomerLevel> qw = new LambdaQueryWrapper<>();
        if (status != null && !status.isEmpty()) {
            qw.eq(CrmCustomerLevel::getStatus, Integer.parseInt(status));
        }
        qw.orderByAsc(CrmCustomerLevel::getSortOrder);
        List<CrmCustomerLevel> list = levelService.list(qw);
        return R.ok(list);
    }

    /**
     * 根据ID查询客户等级
     */
    @GetMapping("/{id}")
    public R<CrmCustomerLevel> get(@PathVariable("id") String id) {
        CrmCustomerLevel level = levelService.getById(Long.parseLong(id));
        return R.ok(level);
    }

    /**
     * 新增客户等级
     */
    @PostMapping
    public R<Boolean> create(@RequestBody CrmCustomerLevel level) {
        return R.ok(levelService.save(level));
    }

    /**
     * 更新客户等级
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable("id") String id, @RequestBody CrmCustomerLevel level) {
        level.setLevelId(Long.parseLong(id));
        return R.ok(levelService.updateById(level));
    }

    /**
     * 删除客户等级
     */
    @DeleteMapping("/{id}")
    public R<Boolean> remove(@PathVariable("id") String id) {
        return R.ok(levelService.removeById(Long.parseLong(id)));
    }

    /**
     * 批量删除客户等级
     */
    @PostMapping("/batch-delete")
    public R<Boolean> batchDelete(@RequestBody List<String> ids) {
        List<Long> longIds = ids.stream().map(Long::parseLong).collect(Collectors.toList());
        return R.ok(levelService.removeByIds(longIds));
    }
}

